package com.binvshe.binvshe.common.huanxin_im;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.binvshe.binvshe.account.AccountManager;
import com.binvshe.binvshe.account.RegisterResponce;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.mine.usercenter.UserCenterActivity;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.model.dao.UserIconHelper;
import com.hyphenate.easeui.ui.EaseChatFragment;
import com.hyphenate.easeui.widget.chatrow.EaseCustomChatRowProvider;
import com.pachong.android.framework.httprequest.UrlParams;

/**
 * 可以直接new出来使用的聊天对话页面fragment，
 * 使用时需调用setArguments方法传入chatType(会话类型)和userId(用户或群id)
 * app也可继承此fragment续写
 * <br/>
 * <br/>
 * 参数传入示例可查看demo里的ChatActivity
 *
 */
public class ChatFragment extends EaseChatFragment implements EaseChatFragment.EaseChatFragmentListener {



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ease_fragment_chat, container, false);
        setChatFragmentListener(this);
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
//        EMClient.getInstance().chatManager().addMessageListener(messageListener);
    }

    @Override
    public void onStop() {
        super.onStop();
//        EMClient.getInstance().chatManager().removeMessageListener(messageListener);
    }

    @Override
    public void onSetMessageAttributes(EMMessage message) {
        message.setAttribute(UserIconHelper.KEY_NICK_NAME, AccountManager.getCurrentAccount().getNickName());
        message.setAttribute(UserIconHelper.KEY_AVATAR, AccountManager.getCurrentAccount().getPortrait());
        EaseChatFragment.log("  onSetMessageAttributes nick name = " + AccountManager.getCurrentAccount().getNickName());
        EaseChatFragment.log("  onSetMessageAttributes avatar = " + AccountManager.getCurrentAccount().getPortrait());
    }

    @Override
    public void onEnterToChatDetails() {
    }

    @Override
    public void onAvatarClick(String username) {

    }

    @Override
    public boolean onMessageBubbleClick(EMMessage message) {
        return false;
    }

    @Override
    public void onMessageBubbleLongClick(EMMessage message) {

    }

    @Override
    public boolean onExtendMenuItemClick(int itemId, View view) {
        return false;
    }

    @Override
    public EaseCustomChatRowProvider onSetCustomChatRowProvider() {
        return null;
    }

    @Override
    protected void setUpView() {
        super.setUpView();

        // 设置标题栏点击事件
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        titleBar.setRightLayoutClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (chatType == EaseConstant.CHATTYPE_SINGLE) {
                    //TODO
                    Log.e("jinlaichatFragment","assdsfdsfdsfds");
//                    emptyHistory();
                    MyHttpRequest request = new MyHttpRequest(getContext());
                    String url = String.format(UrlCenter.GET_USERID, toChatUsername);
                    request.get(url, new UrlParams(), new DataRequestListener<RegisterResponce>(RegisterResponce.class) {
                        @Override
                        public void onSuccess(RegisterResponce o) {
                            super.onSuccess(o);
                            String user_id = o.getUser_id();
                            Intent intent = new Intent(getActivity(), UserCenterActivity.class);
                          intent.putExtra("USER_ID", user_id);
                          startActivity(intent);

                        }

                        @Override
                        public void onFailure(Throwable t, int errorNo, String strMsg) {
                            super.onFailure(t, errorNo, strMsg);
                        }

                        @Override
                        public void onStart(Context context, String url) {
                            super.onStart(context, url);
                        }
                    });


//
                } else {
                    toGroupDetails();
                }
            }
        });


    }
}
